<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Registering operator behaviours</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_reg_opbeh.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Registering operator behaviours </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>In order for AngelScript to know how to work with the application registered types, it is necessary to register some behaviours, for example for memory management.</p>
<p>The memory management behaviours are described with the registeration of registering <a class="el" href="doc_reg_basicref.html">reference types</a> and <a class="el" href="doc_register_val_type.html">value types</a>.</p>
<p>Other advanced behaviours are described with the <a class="el" href="doc_advanced_api.html">advanced types</a>.</p>
<p>Only a few operators have special behaviours for them, the other operators are registered as ordinary <a class="el" href="doc_script_class_ops.html">class methods with predefined names</a>.</p>
<h2><a class="anchor" id="doc_reg_opbeh_2"></a>
Value cast operators</h2>
<p>The value cast operators are used to allow the scripts to convert an object type to another type by constructing a new value. This is different from a <a class="el" href="doc_adv_class_hierarchy.html">reference cast</a>, that do not construct new values, but rather changes the way it is perceived.</p>
<p>By registering the behaviour either as <a class="el" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5ae0b54db9bbd5bf49d0e22cafd886b833">asBEHAVE_VALUE_CAST</a> or <a class="el" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a03d72ae8d0f1e1b4e16f903346a694c6">asBEHAVE_IMPLICIT_VALUE_CAST</a> you let AngelScript know whether the behaviour may be used to implicitly cast the type or not.</p>
<div class="fragment"><pre class="fragment"><span class="comment">// Convert a string to an int</span>
<span class="keywordtype">int</span> ConvStringToInt(<span class="keyword">const</span> std::string &amp;s)
{
  <span class="keywordflow">return</span> atoi(s.c_str());
}

<span class="comment">// Register the behaviour</span>
r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;string&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5ae0b54db9bbd5bf49d0e22cafd886b833" title="Explicit value cast operator.">asBEHAVE_VALUE_CAST</a>, <span class="stringliteral">&quot;int f() const&quot;</span>, <a class="code" href="angelscript_8h.html#a78f8f2c7f1c88b12e74a5ac47b4184ae" title="Returns an asSFuncPtr representing the function specified by the name.">asFUNCTION</a>(ConvStringToInt), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4ac08652c72f1cc0dc81c37812fab0e253" title="A cdecl function that takes the object pointer as the last parameter.">asCALL_CDECL_OBJLAST</a>); assert( r &gt;= 0 );
</pre></div><p>The return type for the cast behaviour can be any type except bool and void. The value cast is meant to create a new value, so if the function returns a reference or an object handle make sure it points to a new value and not the original one.</p>
<p>The object constructors and factories also serve as alternative explicit value cast operators, so if a constructor or factory is already available then there is no need to register the explicit value cast operator. </p>
</div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:04 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
